//
//  XJCmtIdCommitMsgView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/13.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  社区认证提交认证信息view

#import "XJCmtIdCommitMsgView.h"
@interface XJCmtIdCommitMsgView()
@property (nonatomic, strong) UIImageView *imageIv; //头像view


@property (nonatomic, strong) XJLabelTextField * roomNameTF; //1.房间名称
@property (nonatomic, strong) XJLabelTextField * nameTF; //2.业主姓名TF
@property (nonatomic, strong) XJLabelTextField * phoneTF; //3.业主手机号码
@property (nonatomic, strong) XJLabelTextField * relationShipTF; //4.与业主关系

@property (nonatomic, strong) UIButton *submitButton; //提交按钮

@end

@implementation XJCmtIdCommitMsgView
#define xjcmtK15 15.0
#define xjcmtKw (KWidth - xjcmtK15 *2)
#define xjcmtTFsize CGRectMake(0, 0, xjcmtKw, 40)


#pragma mark - 一 lazy
//第一部分
- (UIImageView *)imageIv {
    if (!_imageIv) {
        
        _imageIv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"社区认证.png"]];
        _imageIv.userInteractionEnabled = YES;
    }
    return _imageIv;
}

//第二部分==========================
- (XJLabelTextField *)roomNameTF {
    if (!_roomNameTF) {
        _roomNameTF = [[XJLabelTextField alloc] initWithFrame:xjcmtTFsize keyType:(UIKeyboardTypeNumberPad) textEntry:NO leftTitle:@"房间名称" placeholder:@"如1号楼4-6-4"];
    }
    return _roomNameTF;
}
- (XJLabelTextField *)nameTF {
    if (!_nameTF) {
        _nameTF = [[XJLabelTextField alloc] initWithFrame:xjcmtTFsize keyType:(UIKeyboardTypeDefault) textEntry:NO leftTitle:@"业主姓名" placeholder:@"请输入"];
    }
    return _nameTF;
}
- (XJLabelTextField *)phoneTF {
    if (!_phoneTF) {
        _phoneTF = [[XJLabelTextField alloc] initWithFrame:xjcmtTFsize keyType:(UIKeyboardTypePhonePad) textEntry:NO leftTitle:@"业主手机号" placeholder:@"请输入"];
    }
    return _phoneTF;
}
- (XJLabelTextField *)relationShipTF {
    if (!_relationShipTF) {
        _relationShipTF = [[XJLabelTextField alloc] initWithFrame:xjcmtTFsize keyType:(UIKeyboardTypeDefault) textEntry:NO leftTitle:@"与业主关系" placeholder:@"如本人、亲属称呼"];
    }
    return _relationShipTF;
}
- (UIButton *)submitButton {
    if (!_submitButton) {
        _submitButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_submitButton setTitle:@"提交" forState:(UIControlStateNormal)];
        [_submitButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_submitButton setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];

        [_submitButton addTarget:self action:@selector(submitButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        _submitButton.layer.masksToBounds = YES;
        _submitButton.layer.cornerRadius = 10.0;
        _submitButton.backgroundColor = XJCOLOR_RGB(255, 218, 67);
    }
    return _submitButton;
}


//第三部分==========================
#pragma mark - 二 init

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    [self addSubview:self.imageIv];
    
    [self addSubview:self.roomNameTF];
    [self addSubview:self.nameTF];
    [self addSubview:self.phoneTF];
    [self addSubview:self.relationShipTF];
    
    [self addSubview:self.submitButton];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.imageIv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(xjcmtK15);
        make.right.equalTo(self.mas_right).offset(-xjcmtK15);
        make.top.equalTo(self.mas_top).offset(xjcmtK15);
        make.size.mas_equalTo(CGSizeMake(xjcmtKw, ADAPTHeight(180)));
    }];
    
    [self.roomNameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(xjcmtK15);
        make.right.equalTo(self.mas_right).offset(-xjcmtK15);
        make.top.equalTo(self.imageIv.mas_bottom).offset(xjcmtK15);
        make.height.mas_equalTo(44);
    }];
    [self.nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(xjcmtK15);
        make.right.equalTo(self.mas_right).offset(-xjcmtK15);
        make.top.equalTo(self.roomNameTF.mas_bottom).offset(xjcmtK15);
        make.height.mas_equalTo(44);
    }];
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(xjcmtK15);
        make.right.equalTo(self.mas_right).offset(-xjcmtK15);
        make.top.equalTo(self.nameTF.mas_bottom).offset(xjcmtK15);
        make.height.equalTo(@(44));
    }];
    [self.relationShipTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(xjcmtK15);
        make.right.equalTo(self.mas_right).offset(-xjcmtK15);
        make.top.equalTo(self.phoneTF.mas_bottom).offset(xjcmtK15);
        make.height.equalTo(@(44));
    }];
    
    
    [self.submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(xjcmtK15);
        make.right.equalTo(self.mas_right).offset(-xjcmtK15);
        make.bottom.equalTo(self.mas_bottom).offset(-xjcmtK15 - kStatusBarAndNavigationBarHeight);
        make.height.equalTo(@(44));
    }];
}

#pragma mark - 三 点击事件

- (void)submitButtonAction:(UIButton *)button {
    XJLog(@"");
    
    if([NSString isEmptyString:self.roomNameTF.text]) {
        [JRToast showWithText:@"请输入房间名称"];
        return;
    }
  
    if([NSString isEmptyString:self.nameTF.text]) {
        [JRToast showWithText:@"请输入业主姓名"];
        return;
    }
//    if([NSString isEmptyString:self.phoneTF.text]) {
//        [JRToast showWithText:@"请输入业主手机号"];
//        return;
//    }
    
//    if(![self.roomNameTF.text isPhoneNumber]) {
//        [JRToast showWithText:@"请输入正确的手机号"];
//        return;
//    }
//
//    if([NSString isEmptyString:self.roomNameTF.text]) {
//        [JRToast showWithText:@"请输入与业主的关系"];
//        return;
//    }
    
    [self requestComminity];
}

// 判断 + 网络请求都在这里写算求子

- (void)requestComminity{
    
    NSDictionary *parDic = [self assembleCommunityIdParDic];
    
    [XJHttpTool requestCommunityIdentityWithParDic:parDic successH:^(id responseObjc) {
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            
            // 验证通过发送网络请求 请求成功 回调结果给控制器(这个block走了就说明请求成功了)
            if (self.xjcmtIdCommitMsgViewClickSubmitBtnBlock) {
                self.xjcmtIdCommitMsgViewClickSubmitBtnBlock();
            }
            
        }else {
            [JRToast showWithText:responseObjc[@"message"]];
        }
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}

//组装请求参数
- (NSDictionary *)assembleCommunityIdParDic {
    NSString * community_id = self.community_id;
    NSString *room_name = self.roomNameTF.text;
    NSString *ptName = self.nameTF.text;
    NSString *ptPhone = self.phoneTF.text;
    NSString *relation = self.relationShipTF.text;
    
    NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
    if (![NSString isEmptyString:community_id]) { //社区id请求参数 ✅
        mDic[@"community_id"] = community_id;
    }
    if (![NSString isEmptyString:room_name]) {
        mDic[@"room_name"] = room_name;
    }
    if (![NSString isEmptyString:ptName]) {
        mDic[@"proprietor_name"] = ptName;
    }
    if (![NSString isEmptyString:ptPhone]) {
        mDic[@"proprietor_phone"] = ptPhone;
    }
    if (![NSString isEmptyString:relation]) {
        mDic[@"proprietor_relation"] = relation;
    }
    
    XJLog(@"社区认证请求参数:%@",mDic);
    
    return [mDic copy];
}

//回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignFirstResponder];
    [self endEditing:YES];
}
@end
